var userDataPageSize = 10;

//读取数据
function createList(pageNo = 1, pageSize = 10) {
    let matchData = { 'isRobot': { '$ne': true } };
    let uid = $("#userIDTxt").val();
    if (!!uid) {
        matchData.uid = parseInt(uid);
    }
    let isOnLine = $("#onLineUser").val();
    if (isOnLine == '1') {
        matchData.frontendId = { $ne: "" };
    }

    var start = (pageNo - 1) * pageSize;
    apiAdminGetRecord("userModel", start, pageSize, matchData, function (data) {
        if (data.code === 0) {
            setTimeout(() => {
                $('#userList').bootstrapTable('load', data.msg);
            }, 200)

        }
    });
}

//禁止登录
function forbidLogin(uid, isBlockedAccount, forbid) {
    if (!!forbid && !!isBlockedAccount) {
        $.modal({ body: '操作成功' });
        return;
    }
    if (!forbid && !isBlockedAccount) {
        $.modal({ body: '操作成功' });
        return;
    }
    apiUpdateUserData(uid, { isBlockedAccount: !!forbid ? 1 : 0 }, function () {
        $.modal({ body: '操作成功' });
        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
        createList(currentPage);
    });
}

//允许禁止调试
function setTest(uid, userTest, isTest) {

    if (!!isTest && !!userTest) {
        $.modal({ body: '操作成功' });
        return;
    }
    if (!isTest && !userTest) {
        $.modal({ body: '操作成功' });
        return;
    }
    apiUpdateUserData(uid, { isTest: isTest }, function () {
        $.modal({ body: '操作成功' });
        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
        createList(currentPage);
    });
}

//显示新增金币
function showAddGold(uid) {
    $("#goldUid").val(uid);
    $("#goldCount").val("")
    $('#addGoldDig').modal('show');
}


function showUserType(uid, type = 0) {
    $("#userUid").val(uid);
    $("#userType").val(type)
    $('#editUserType').modal('show');
}

function addGold() {

    let uid = $("#goldUid").val();
    let count = $("#goldCount").val() || 0;
    execGrant('gold', uid, count);
}

function editType() {

    let uid = $("#userUid").val();
    let type = $("#userType").val() || 0;
    apiUpdateUserData(uid, { userType: type }, function () {
        $('#editUserType').modal('hide');
        $.modal({ body: '操作成功' });
        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
        createList(currentPage);
    });
}


//允许禁止创建联盟
function setAgent(uid, userAgent, isAgent) {

    if (!!isAgent && !!userAgent) {
        $.modal({ body: '操作成功' });
        return;
    }
    if (!isAgent && !userAgent) {
        $.modal({ body: '操作成功' });
        return;
    }
    apiUpdateUserData(uid, { isAgent: isAgent }, function () {
        $.modal({ body: '操作成功' });
        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
        createList(currentPage);

    });
}



function execGrant(field, uid, count) {
    count = parseInt(count) || 0;
    if (count === 0) {
        return;
    }
    apiAdminGrant(field, uid, count, () => {
        $('#addGoldDig').modal('hide');
        $.modal({ body: '操作成功' });
        var currentPage = $('#userList').bootstrapTable('getOptions').pageNumber
        createList(currentPage);
    })
}

$(document).ready(function () {

    //数据表格初始化
    $('#userList').bootstrapTable({
        striped: true, // 是否显示行间隔色
        pageNumber: 1, // 初始化加载第一页
        pagination: true, // 是否分页
        sidePagination: 'server', // server:服务器端分页|client：前端分页
        pageSize: 10, // 单页记录数
        pageList: [10, 20, 30],
        theadClasses: "thead-light",//这里设置表头样式
        totalField: "totalCount",
        smartDisplay: false,
        buttonsClass: 'light',
        buttonsPrefix: 'btn',
        dataField: "recordArr",
        showRefresh: false,// 刷新按钮
        onPageChange: function (number, size) {
            createList(number, size);
        },
        queryParams: function (params) { // 上传服务器的参数
            var temp = {//如果是在服务器端实现分页，limit、offset这两个参数是必须的
                limit: params.limit, // 每页显示数量
                offset: params.offset, // SQL语句起始索引
            };
            return temp;
        },
        onPostBody: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        onLoadSuccess: function () {
            //重新启用弹出层,否则formatter格式化函数返回的html字符串上的tooltip提示不生效
            $('[data-bs-toggle="tooltip"]').each(function (i, el) {
                new bootstrap.Tooltip(el)
            })
        },
        columns: [{
            title: 'id',
            field: '_id',
            visible: false
        },
        {
            title: 'UID',
            width: 150,
            align: "center",
            valign: "middle",
            field: 'uid',
        }, {
            title: '昵称',
            width: 200,
            align: "center",
            valign: "middle",
            field: 'nickname',
        }, {
            title: '头像',
            width: 150,
            align: "center",
            valign: "middle",
            field: 'avatar',
            formatter: avaterImg
        },
        {
            field: 'gold',
            title: '钻石数',
            align: "center",
            valign: "middle",
            width: 150,
            formatter: function (value) {
                if (!!value || value === 0) {
                    return parseInt(value);
                }
            }
        },
        // {
        //     field: 'account',
        //     align: "center",
        //     valign: "middle",
        //     title: '绑定账号',
        //     width: 100,
        // },

        {
            field: 'lastLoginIP',
            align: "center",
            valign: "middle",
            width: 200,
            title: '最后登录IP'
        },
        {
            field: 'location',
            align: "center",
            valign: "middle",
            title: '最后定位'
        },
        {
            field: 'lastLoginTime',
            align: "center",
            valign: "middle",
            width: 200,
            title: '最后登录时间',
            formatter: function (value) {
                return new Date(value).toLocaleString();
            }
        },
        {
            field: 'createTime',
            align: "center",
            valign: "middle",
            width: 200,
            title: '创建时间',
            formatter: function (value) {
                return new Date(value).toLocaleString();
            }
        },
        {
            field: 'isAgent',
            align: "center",
            valign: "middle",
            title: '创建联盟',
            width: 50,
            formatter: function (value, row, index) {

                var htm = "";
                if (value) {
                    htm = `<div id="switch1"  onClick='setAgent(${row.uid},${row.isAgent},false)' class="simple-switch active"><input type="checkbox" /> <span class="switch-handler"></span> </div>`;
                } else {
                    htm = `<div id="switch1"  onClick='setAgent(${row.uid},${row.isAgent},true)' class="simple-switch"><input type="checkbox" /> <span class="switch-handler"></span> </div>`;
                }


                return htm;
            }
        },
        // {
        //     field: 'isBlockedAccount',
        //     align: "center",
        //     valign: "middle",
        //     title: '禁止登录',
        //     width: 50,
        //     formatter: function (value, row, index) {
        //         var htm = "";
        //         if (value) {
        //             htm = `<div id="loginS"  onClick='forbidLogin(${row.uid},${row.isBlockedAccount},0)' class="simple-switch active"><input type="checkbox" /> <span class="switch-handler"></span> </div>`;
        //         } else {
        //             htm = `<div id="loginS"  onClick='forbidLogin(${row.uid},${row.isBlockedAccount}, 1)' class="simple-switch "><input type="checkbox" /> <span class="switch-handler"></span> </div>`;
        //         }
        //         return htm;
        //     }
        // },
        {
            title: '操作',
            width: 200,
            align: "center",
            valign: "middle",
            formatter: function (value, row, index) {

                let htm = ``;
                htm += `<button type = "button" class="btn  btn-sm btn-success"  data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="赠送钻石" onClick = 'showAddGold(${row.uid})' >赠送钻石</i></button > `;
                // htm += `<button type = "button" class="btn btn-light" data-bs-toggle="tooltip" data-bs-placement="top" data-bs-title="设置黑白名单" onClick = 'showUserType(${row.uid}, ${row.userType})' ><i class="bi bi-person-fill-gear fs-5 text-success"></i></button > `;
                return htm;
            }
        }]
    });
    createList();
});

function avaterImg(value, row, index) {
    var htm = "";
    htm = `<img  style="width: 40px ; height: 40px;"  src="${value}" class="img-thumbnail" >`;
    return htm;
}